#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
    echo -e "${ERROR} RALAT ${RESET}${TEXT} You need to run this script as root! ${RESET}"; exit 1
fi

function installPkg {
    clear
    read -p "Teruskan dengan pemasangan semula [Y/n]? " _yesno
    if [[ "$_yesno" =~ ^[Yy]$ ]]; then
        if [[ ! -f /etc/default/dropbear ]]; then
            wget -q -O - 'https://raw.githubusercontent.com/cybertize/doctype/default/packages/dropbear.sh' | bash
        fi
        {
            systemctl stop dropbear
            apt-get -y -qq --purge remove dropbear
            apt-get -y -qq autoremove
            apt-get -qq autoclean
        } &>/dev/null

        clear && sleep 3
        wget -q -O - 'https://raw.githubusercontent.com/cybertize/doctype/default/packages/dropbear.sh' | bash
    else
        echo -e "${ERROR} RALAT ${RESET}${TEXT} Membatalkan pemasangan semula!.. ${RESET}"; exit 1
    fi
}

function uninstallPkg {
    clear
    read -p "Adakah anda pasti mahu menyahpasang dropbear? [Y/n] " _yesno
    if [[ "$_yesno" =~ ^[Yy]$ ]]; then
    {
        apt-get -y -qq --purge remove dropbear
        apt-get -y -qq autoremove
        apt-get -qq autoclean
    } &>/dev/null
        
        echo -e "${SUCCESS} BERJAYA ${RESET}${TEXT} Bersihkan & Keluarkan pakej dropbear ${RESET}"; exit 0
    else
        echo -e "${ERROR} RALAT ${RESET}${TEXT} Dibatalkan menyahpasang!.. ${RESET}"; exit 1
    fi
}

function configurePkg {
    clear
    dropbear_port=$(cat /etc/default/dropbear | grep -w 'DROPBEAR_PORT' | cut -d '=' -f 2)
    dropbear_ports=$(cat /etc/default/dropbear | grep -w 'DROPBEAR_EXTRA_ARGS' | cut -d '"' -f 2 | grep -w '\-p' | cut -d ' '  -f 2,4,6)
    declare dropbear_ports

    echo
    echo "Secara lalai perkhidmatan dropbear mendengar pada port $dropbear_port"
    echo "dan port ini digunakan untuk sambungan stunnel"
    echo
    while true; do
        read -r -p "Adakah anda mahu menukar port [Y/n]? " _yesno
        case $_yesno in
            [Yy])
                until [[ $input_port =~ ^[0-9]+$ ]]; do
                    read -p "Masukkan port baharu: " input_port
                    if [[ -n $input_port ]]; then
                        systemctl stop dropbear; systemctl stop stunnel4
                        sed -i "s|${dropbear_port}|${input_port}|g" /etc/default/dropbear
                        sed -i "s|${dropbear_port}|${input_port}|g" /etc/stunnel/stunnel.conf

                        echo -e "${SUCCESS} BERJAYA ${RESET}${TEXT} Perubahan telah dibuat ${RESET}"
                    else
                        echo -e "${ERROR} RALAT ${RESET}${TEXT} Dibatalkan!... ${RESET}"; exit 1
                    fi
                done
                systemctl restart dropbear; systemctl restart stunnel4; break
            ;;
            [Nn])
                echo -e "${SUCCESS} INFO ${RESET}${TEXT} Tiada perubahan dibuat ${RESET}"; break
            ;;
        esac
    done

    echo
    echo "By default dropbear sevice use 3 extra ports $dropbear_ports if you"
    echo "want to change more then one port, you need to seperated them"
    echo "with [space] e.g. $dropbear_ports"
    echo
    while true; do
        read -r -p "Do you want to change port [Y/n]? " _yesno
        case $_yesno in
            [Yy])
                read -p "Enter new ports: " first_port second_port third_port
                if [[ -n $first_port && $first_port =~ ^[0-9]+$ ]]; then
                    sed -i "s|${PORTS[0]}|$first_port|g" /etc/default/dropbear
                elif [[ -n $second_port && $second_port =~ ^[0-9]+$ ]]; then
                    sed -i "s|${PORTS[1]}|$second_port|g" /etc/default/dropbear
                elif [[ -n $third_port && $third_port =~ ^[0-9]+$ ]]; then
                    sed -i "s|${PORTS[2]}|$third_port|g" /etc/default/dropbear
                else
                    echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}" && exit 1
                fi

                unset dropbear_ports
                systemctl restart dropbear && systemctl restart stunnel4
                echo -e "${INFO} INFO ${RESET}${TEXT} Changes have been made ${RESET}"; break
            ;;
            [Nn])
                echo -e "${INFO} INFO ${RESET}${TEXT} No changes made ${RESET}"; break
            ;;
        esac
    done
}

function detailPkg {
    clear
    local unit_id=$(systemctl show dropbear.service -p Id | cut -d '=' -f 2)
    local unit_name=$(systemctl show dropbear.service -p Names | cut -d '=' -f 2)
    local unit_desc=$(systemctl show dropbear.service -p Description | cut -d '=' -f 2)
    local is_active=$(systemctl is-active dropbear.service)
    local is_enable=$(systemctl is-enabled dropbear.service)
    local dropbear_port=$(cat /etc/default/dropbear | grep -w 'DROPBEAR_PORT' | cut -d '=' -f 2)
    local dropbear_ports=$(cat /etc/default/dropbear | grep -w 'DROPBEAR_EXTRA_ARGS' | cut -d '"' -f 2 | grep -w '\-p' | cut -d ' '  -f 2,4,6)

    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} Butiran perkhidmatan Dropbear                              ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "${YELLOW}     ID:${RESET} ${GREEN}$unit_id${RESET}"
    echo -e "${YELLOW}   Name:${RESET} ${GREEN}$unit_name${RESET}"
    echo -e "${YELLOW}   Desc:${RESET} ${GREEN}$unit_desc${RESET}"
    echo -e "${YELLOW} Status:${RESET} ${GREEN}$is_active & $is_enable${RESET}"
    echo -e "${YELLOW}  Ports:${RESET} ${GREEN}$dropbear_port $dropbear_ports${RESET}";
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
    echo -e "${TEXT} Hak Cipta 2021, Hak Cipta Terpelihara.                     ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
}

function dropbearMenu {
    clear; echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} MENU DROPBEAR                                              ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "[01] ${GREEN}reinstall${RESET}  - ${YELLOW}Reinstall dropbear package${RESET}"
    echo -e "[02] ${GREEN}uninstall${RESET}  - ${YELLOW}Uninstall dropbear package${RESET}"
    echo -e "[03] ${GREEN}configure${RESET}  - ${YELLOW}Edit/Configure dropbear config${RESET}"
    echo -e "[04] ${GREEN}detail   ${RESET}  - ${YELLOW}Show dropbear detail & status${RESET}"
    echo -e "[00] ${GREEN}quit     ${RESET}  - ${YELLOW}Exit from menu${RESET}"
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
    echo -e "${TEXT} Hak Cipta 2021, Hak Cipta Terpelihara.                     ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
    while true; do
        read -p "Masukkan pilihan anda: " _choose
        case "$_choose" in
            01|reinstall)
                installPkg && break
            ;;
            02|uninstall)
                uninstallPkg && break
            ;;
            03|configure)
                configurePkg && break
            ;;
            04|detail)
                detailPkg && break
            ;;
            00|quit)
                exit 0
            ;;
            *)
                echo -e "${ERROR} RALAT ${RESET}${TEXT} Pilihan tidak sah! ${RESET}"; dropbearMenu
            ;;
        esac
    done
}

if [[ -f /etc/default/dropbear ]]; then
    dropbearMenu
elif [[ ! -f /etc/default/dropbear ]]; then
    installPkg
fi